<?php namespace Admin\Model;

use Hdphp\Model\Model;

/**
 * 商品管理模型
 */
class Goods extends Model{

    protected $table = "shop_goods";

    //自动验证
    protected $validate = array( 
        array('goods_name','required','商品名不能为空',3,3),
    ); 
    //自动完成
    protected $auto=array(
        array('create_at','time','function',3,1),
        array('admin_id','getAdminId','method',3,1),
        array('list_pic','getListPic','method',3,3),
        array('thumb_pic','getThumbPic','method',3,3),
    );
    
    //自动完成，获取商品的管理员ID
    protected function getAdminId()
    {
        return $_SESSION['admin_id'];
    }

    //自动完成，获取商品的列表页图
    protected function getListPic()
    {
        if($file=Upload::make('list_pic'))
        {
            return $file[0]['path'];
        }
        //简写版三元式，Q函数版
        return Q('post.list_pic','');
    }
    //自动完成，获取商品的缩略图
    protected function getThumbPic()
    {
         if($file=Upload::make('thumb_pic'))
        {
            return $file[0]['path'];
        }
        //简写版三元式，Q函数版
        return Q('post.thumb_pic','');
    }
    //获取所有商品
    public function getAll($cat_id='')
    {
        if($cat_id)
        {
            //读取指定栏目ID的商品
            $page=Page::make($this->where('cat_id',$cat_id)->count());
            $data=Db::table('shop_goods g')->where('c.cat_id',$cat_id)->limit(Page::limit())
            ->join('shop_cat c','g.cat_id','=','c.cat_id')->get();
        }
        else
        {
            //读取所有商品
            $page=Page::make($this->count());
            $data=Db::table('shop_goods g')->limit(Page::limit())->join('shop_cat c','g.cat_id','=','c.cat_id')
            ->get();
        }
        return array('data'=>$data,'page'=>$page);
    }

    //添加商品
    public function store()
    {
        if($this->create())
        {   
            if($goods_id=$this->add())
            {
                //图集处理
                $pics=new \Admin\Model\Pics;
                $pics->store($goods_id);
                //商品属性处理
                $goodsAttr=new \Admin\Model\goodsAttr;
                $goodsAttr->addGoodsAttr($goods_id);
            }
            return true;
        }
    }

    //修改商品
    public function edit()
    {
        if($this->create())
        {
            if($this->save())
            {
                $pics=new \Admin\Model\Pics;
                $pics->store($_POST['goods_id']);
            }
            return true;
        }
    }

    //获取商品信息
    public function one($goods_id)
    {
        return $this->find($goods_id);
    }


    //根据选择的类型获取商品的属性列表
    public function getAttrList($type_id)
    {
        //所有属性
        $attrs=Db::table('shop_attr')->orderBy('orderBy','ASC')->where('shop_type_id',$type_id)
        ->get();
        $html='';
        foreach($attrs as $a)
        {
            //规格属性时添加”加价“表单
            switch($a['attr_type'])
            {
                case 1:
                //普通属性
                $priceField='<input type="hidden" name="add_price[]" value="0">';
                break;
                case 2:
                //规格属性
                $priceField='加价:
                <input type="text" name="add_price[]"> <span class="addPrice">[+]</span>';
                break;
            }

            //组合属性的html表单
            switch ($a['show_type']) {
                case 1:
                    # 文本框
                    $field =<<<php
         <tr>
            <td width="150" align="right">{$a['attr_name']}</td>
            <td>
              <input type="hidden" name="attr_id[]" value="{$a['attr_id']}">
              <input type="text" name="attr_value[]">
              $priceField
            </td>
          </tr><br/>
php;
                    break;
                
                case 2:
                    # 列表框
                    $attrValue = preg_split('@\n@',$a['attr_value']);
                    $options="";
                    foreach($attrValue as $opt)
                    {
                        $options.='<option value="'.$opt.'">'.$opt.'</option>';
                    }
                    $field=<<<php
                    <tr>
                        <td width="150" align="right">{$a['attr_name']}</td>
                        <td>
                        <input type="hidden" name="attr_id[]" value="{$a['attr_id']}">
                        <select name="attr_value[]">
                        {$options}
                        </select>
                        $priceField
                        </td>
                    </tr><br/>
php;
                    break;
            }
            $html.=$field;
        }
        return $html;
    }

}